package com.net.oil.order;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.gaode.RoutePlanActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.order.bean.ReserveDetailBean;
import com.net.oil.order.contract.ReserveDetailContract;
import com.net.oil.order.presenter.ReserveDetailPresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;
import com.net.oil.widget.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReserveDetailActivity extends BaseActivity<ReserveDetailPresenter> implements ReserveDetailContract.View {

    @BindView(R.id.reserve_status_tv)
    TextView reserveStatusTv;
    @BindView(R.id.reserve_time_tv)
    TextView reserveTimeTv;
    @BindView(R.id.car_info_tv)
    TextView carInfoTv;
    @BindView(R.id.take_me_to_layout)
    LinearLayout takeMeToLayout;
    @BindView(R.id.shop_name)
    TextView shopName;
    @BindView(R.id.shop_address)
    TextView shopAddress;
    @BindView(R.id.right_icon)
    ImageView rightIcon;
    @BindView(R.id.shop_phone)
    TextView shopPhone;
    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.shop_phone_layout)
    RelativeLayout shopPhoneLayout;
    @BindView(R.id.cancel_button)
    Button cancelButton;
    @BindView(R.id.shop_logo)
    ImageView shopLogo;

    private ReserveDetailBean reserveDetailBean;
    private int id;     //预约id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reserve_detail);
        ButterKnife.bind(this);
        presenter = new ReserveDetailPresenter(this);
        initData();
        initView();
    }

    private void initData() {
        id = getIntent().getIntExtra(Constant.INTENT_RESERVE_ID_KEY, -1);
        if (id != -1) {
            showProgressDialog();
            presenter.getReserveDetail(id);
        }
    }

    private void initView() {
        toolbarTitle.setText(getString(R.string.reserve_detail));
        toolbarGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 获取预约详情成功
     *
     * @param reserveDetailBean 预约详情
     */
    @Override
    public void getReserveDetailSuccess(ReserveDetailBean reserveDetailBean) {

        this.reserveDetailBean = reserveDetailBean;
        dismissProgressDialog();
        if (reserveDetailBean != null) {
            if (!StringUtil.isEmpty(reserveDetailBean.getAppointmentTime())) {
                reserveTimeTv.setText(reserveDetailBean.getAppointmentTime());
            }
            if (!StringUtil.isEmpty(reserveDetailBean.getCarModel())) {
                carInfoTv.setText(reserveDetailBean.getCarModel());
            }

            if (!StringUtil.isEmpty(reserveDetailBean.getBusinessName())) {
                shopName.setText(reserveDetailBean.getBusinessName());
            }

            /*
             * status状态代表预约状态（expireStatus为0的情况下，1：已预约 0：用户取消预约 2:已到店（完成），expireStatus为1的情况下，就是已过期)
             */
            if (reserveDetailBean.getExpireStatus() == 0) {
                if (reserveDetailBean.getStatus() == Constant.RESERVE_STATUS_SUCCESS) {
                    reserveStatusTv.setText("已预约");
                    cancelButton.setVisibility(View.VISIBLE);

                } else if (reserveDetailBean.getStatus() == Constant.RESERVE_STATUS_CANCEL) {
                    reserveStatusTv.setText("已取消");
                } else if (reserveDetailBean.getStatus() == Constant.RESERVE_STATUS_FINSH) {
                    reserveStatusTv.setText("已完成");
                }
            } else if (reserveDetailBean.getExpireStatus() == 1) {
                reserveStatusTv.setText("已过期");
            }

            ReserveDetailBean.OilBusinessBean oilBusinessBean = reserveDetailBean.getOilBusiness();
            if (oilBusinessBean != null) {
                if (!StringUtil.isEmpty(oilBusinessBean.getAddress())) {
                    shopAddress.setText(oilBusinessBean.getAddress());
                }
                if (!TextUtils.isEmpty(oilBusinessBean.getLogo())){
                    GlideUtil.getInstance().loadCircleImage(this, shopLogo, AppUrl.getImageUrl(oilBusinessBean.getLogo()), R.drawable.shop_err);
                }
                if (!StringUtil.isEmpty(oilBusinessBean.getPhone())) {
                    String str = "联系商家：" + oilBusinessBean.getPhone();
                    shopPhone.setText(str);
                }
            }


        }
    }

    /**
     * 获取预约详情失败
     */
    @Override
    public void getReserveDetailFailed() {
        dismissProgressDialog();
    }

    /**
     * 取消预约成功
     */
    @Override
    public void cancelReserveSuccess() {
        dismissProgressDialog();
        reserveStatusTv.setText("已取消");
        cancelButton.setVisibility(View.GONE);
        ToastUtil.showSuccessToast(this, "取消成功");
    }

    /**
     * 取消预约失败
     */
    @Override
    public void cancelReserveFailed() {
        dismissProgressDialog();
        ToastUtil.showErrorToast(this, "取消预约失败");
    }

    /**
     * 拨打电话
     *
     * @param phoneNumber 电话号码
     */
    private void showCallDialog(final String phoneNumber) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("联系商家");
        builder.setMessage(phoneNumber);
        builder.setPositiveButton("联系商家", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Uri uri = Uri.parse("tel:" + phoneNumber);
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    @OnClick({R.id.take_me_to_layout, R.id.shop_phone_layout, R.id.cancel_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.take_me_to_layout:
                Intent intent = new Intent(this, RoutePlanActivity.class);
                if (reserveDetailBean != null) {
                    ReserveDetailBean.OilBusinessBean oilBusinessBean = reserveDetailBean.getOilBusiness();
                    if (oilBusinessBean != null) {
                        intent.putExtra(Constant.INTENT_LNG, oilBusinessBean.getLng());
                        intent.putExtra(Constant.INTENT_LAT, oilBusinessBean.getLat());
                    }
                }
                startActivity(intent);
                break;
            case R.id.shop_phone_layout:
                if (reserveDetailBean != null) {
                    ReserveDetailBean.OilBusinessBean oilBusinessBean = reserveDetailBean.getOilBusiness();
                    if (oilBusinessBean != null) {
                        if (!StringUtil.isEmpty(oilBusinessBean.getPhone())) {
                            showCallDialog(oilBusinessBean.getPhone());
                        }
                    }
                }
                break;
            case R.id.cancel_button:
                if (id != -1) {
                    new AlertDialog.Builder(this).setTitle("提示")
                            .setMessage("您确定取消预约吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    showProgressDialog();
                                    presenter.cancelReserve(id);
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            }).show();

                }
                break;
        }
    }
}
